package com.dqx.haoqiu.mvp.module.launch;

import android.widget.TextView;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.common.base.BasePresenter;
import com.dqx.haoqiu.repository.repository.user.UserRepository;
import com.xmjs.baselibrary.network.retrofit.RxUtil;
import com.xmjs.baselibrary.utils.SP;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.xmjs.baselibrary.network.retrofit.RxUtil.bindToLifecycle;

public class LaunchPresenter extends BasePresenter<LaunchContract.View> implements LaunchContract.Presenter {
    UserRepository userRepository;

    @Inject
    public LaunchPresenter(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public boolean showPrivaceDialog() {
        return userRepository.showPrivaceDialog();
    }

    @Override
    public void setShowPrivaceDialog(boolean show) {
        userRepository.setShowPrivaceDialog(show);
    }

    @Override
    public boolean showGuide() {
        return userRepository.showGuide();
    }

    @Override
    public void countdown(long duration, TextView timer) {
        //倒计时任务
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .compose(RxUtil.applySchedulers())
                .compose(bindToLifecycle(view))
                .take(duration)
                .map(aLong -> duration - aLong)
                .doOnSubscribe((d) -> {

                })
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        //动态显示倒计时数字
                        timer.setText(timer.getContext().getString(R.string.pass_time, aLong));
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {//循环结束
                        view.countdownFinish();
                    }
                });
    }

}